-- UIMisticCave
-- Created by zouyb Sep/25/2015
-- 神秘山洞

UIMisticCave = class("UIMisticCave", function()
    return cc.Layer:create();
end);

function UIMisticCave.create(gridData)
    return UIMisticCave.new(gridData);
end

-- 按钮数量
local BTN_COUNT = 4;

-- 构造函数
function UIMisticCave:ctor(gridData)
    self:setName("UIMisticCave");

    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView4.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.gridData  = gridData;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIMisticCave:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIMisticCave:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIMisticCave 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIMisticCave:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIMisticCave");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

	-- 按钮标题
	local buttonTitles = { getLocStr("open_sesame"), getLocStr("open_wheat"), getLocStr("open_barley"), getLocStr("open_soybean") };

    -- 注册按钮点击事件
    self.buttons = {};
    for i = 1, BTN_COUNT do
        local btn = findChildByName(node, "CT/btn"..i);
        table.insert(self.buttons, btn);
        local function onOkClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_select");

				local ret = DungeonActionM.go("mistic_cave", self.gridData:getPos(), i);

				if i ~= 1 then
                    if true == ret then
					   alert(getLocStr("you_are_found"));
					else
                       alert(getLocStr("nothing_happen_to_cave"));
                    end
				else
					alert(getLocStr("treaure_found"));
				end

				UIMgr.getCurrentScene():removeFormByName("UIMisticCave");
            end
        end
        AddTouchEventListener(btn, onOkClick);
        TextStyleM.setTextStyle(btn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        btn:setTitleText(buttonTitles[i]);
    end
end

-- 重绘
function UIMisticCave:redraw()
    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    titleLabel:setString(buildingInfo["title"]);
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    iconImg:loadTexture(iconPath);
    local iconSize = iconImg:getContentSize();

    iconImg:setScale(1.3);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    applyString(descLabel, buildingInfo["dialog"]);
end






